<?php
require_once 'simplegraph.class.php';
require_once 'fieldpredicatemap.class.php';
require_once 'constants.inc.php';

class Config {

  var $uri;
  var $request_factory;
  var $credentials;

  function Config($uri, $credentials = null) {
    $this->uri = $uri;
    $this->credentials = $credentials;
  }

  function retrieve_first_fpmap() {
    if (! isset( $this->request_factory) ) {
      $this->request_factory = new HttpRequestFactory();
    }

    $uri = $this->get_first_fpmap_uri();
    $mimetype = MIME_RDFXML;

    $request = $this->request_factory->make('GET', $uri);
    $request->set_accept(MIME_RDFXML);
    if  ($this->credentials != null) {
      $request->set_auth( $this->credentials->get_basic_search_auth() );
    }
    $response = $request->execute();

    $fpmap = new FieldPredicateMap($uri);
    if ( $response->body) {
      $fpmap->from_rdfxml($response->body);
    }

    return $fpmap;
  }

  function replace_first_fpmap($fpmap) {
    if (! isset( $this->request_factory) ) {
      $this->request_factory = new HttpRequestFactory();
    }

    $uri = $this->get_first_fpmap_uri();
    $mimetype = MIME_RDFXML;

    $request = $this->request_factory->make('PUT', $uri);
    $request->set_accept('*/*');
    $request->set_content_type($mimetype);
    $request->set_body( $fpmap->to_rdfxml() );
    if  ($this->credentials != null) {
      $request->set_auth( $this->credentials->get_configure_store_auth() );
    }
    return $request->execute();
  }

  /**
   * Gets the URI of the first field/predicate map in the store
   * This is much more complicated than first appears since a store
   * can be configured (by Talis) to hold its field/predicate map
   * anywhere. This method understands all the existing stores and their
   * URI layouts.
   *
   * @return string
   * @author Ian Davis
   **/
  function get_first_fpmap_uri() {
    if (preg_match("/^http:\/\/api\.talis\.com\/stores\/([a-z][a-zA-Z0-9-]+)\/config$/", $this->uri, $matches) ) {
      $store_name = $matches[1];    
    
      if ( in_array($store_name, array('ajmg-dev1','beobal-dev1', 'danja-dev1', 'dataMonitoring', 'iand-dev1', 'iand-dev2', 'iand-dev3', 'jingye-dev1', 'kwijibo-dev1', 'malcyl-dev1', 'quoll-dev1', 'schema-cache', 'silkworm-dev', 'silkworm', 'source-dev1', 'source-qa1', 'tomh-dev1')) ) {
         return 'http://api.talis.com/stores/' . $store_name . '/indexes/default/fpmaps/default';
      }
      else if ( preg_match("/^engage-dev\d+$/", $store_name) 
                || preg_match("/^engagetenant\d+$/", $store_name)  
                || preg_match("/^list-demo\d+$/", $store_name) 
                || preg_match("/^list-dev\d+$/", $store_name) 
                || preg_match("/^list-qa\d+$/", $store_name)  
                || preg_match("/^nuggetengage-demo\d+$/", $store_name)    
                || preg_match("/^nuggetengage-qa\d+$/", $store_name) 
                || preg_match("/^zephyr-cust\d+$/", $store_name)    
                || $store_name == 'engagetenantstore'  
                || $store_name == 'list-tenants-dev'   ) {
         return 'http://api.talis.com/stores/' . $store_name . '/indexes/metaboxIndex/fpmaps/fpmap';
      }
      else if ( in_array($store_name, array('bib-sandbox', 'inst-5050', 'inst-u138', 'ukbib')) ) {
         return 'http://api.talis.com/stores/' . $store_name . '/indexes/m21Advanced/fpmaps/fpmap';
      }
      else if ( in_array($store_name, array('holdings')) ) {
         return 'http://api.talis.com/stores/' . $store_name . '/indexes/m21Holdings/fpmaps/fpmap';
      }
      else if ( in_array($store_name, array('union')) ) {
         return 'http://api.talis.com/stores/' . $store_name . '/indexes/union/fpmaps/fpmap';
      }
      else if ( in_array($store_name, array('wikipedia')) ) {
         return 'http://api.talis.com/stores/' . $store_name . '/indexes/abstracts/fpmaps/fpmap';
      }
      else if ( in_array($store_name, array('gatech')) ) {
         return 'http://api.talis.com/stores/' . $store_name . '/indexes/m21Advanced/fpmap';
      }
      else if ( in_array($store_name, array('cenotelist')) ) {
         return 'http://api.talis.com/stores/' . $store_name . '/indexes/default/fpmaps/fpmap';
      }
      else if ( in_array($store_name, array('image-sandbox')) ) {
         return 'http://api.talis.com/stores/' . $store_name . '/indexes/image-sandbox/fpmaps/fpmap';
      }
      else if ( in_array($store_name, array('cnimages')) ) {
         return 'http://api.talis.com/stores/' . $store_name . '/indexes/cnimages/fpmaps/fpmap';
      }
    }
    
    return $this->uri . '/fpmaps/1';

  }
}

?>
